package com.hax2max.view;
import java.util.ArrayList;
import java.util.HashMap;

import android.R.drawable;
import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Paint.Align;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.ScrollView;
import android.widget.TextView;
import android.graphics.drawable.GradientDrawable;
import com.hax2max.R;
import com.hax2max.model.FriendProspect;

public class ManageFriendsViewController extends Activity{
	
	final static int[] rowGradientColors = {Color.BLACK,Color.DKGRAY};
	private final static int WORKING = 0;
	private final static int READY = 1;
	protected Handler messageHandler;
	protected ArrayList<FriendProspect> friendList;
	protected static FakedataHandler dh;
	protected static ProgressDialog dialog;
	protected Context _context;
	protected LinearLayout ll;
	private EditText searchBox;
	public void onCreate(Bundle savedInstanceState)
    {
	_context = this.getBaseContext();
	super.onCreate(savedInstanceState);
	setContentView(R.layout.managefriends);
    dh = new FakedataHandler();
	Button searchButton = (Button)findViewById(R.id.ManageFriendsSearchButton);
	searchBox = (EditText)findViewById(R.id.ManageFriendsViewControllerSearchbox);
	ll = (LinearLayout) findViewById(R.id.ManageFriendsViewControllerLinearLayout);
	searchButton.setOnClickListener(new OnClickListener() {
		
		public void onClick(View v) {
			
			
			new Thread() {
				public void run()
				{
					
					getFriendList();
				}
			}.start();
		}
	});
	
	messageHandler = new Handler() {
	    public void handleMessage(Message msg) {
		switch(msg.what){
		case WORKING:
			showDialog(0);
		    break;
		case READY:
			dialog.dismiss();
			displayList();
		   }

	    }
	};
	
	
    }

	protected Dialog onCreateDialog(int id)
    {
		dialog = new ProgressDialog(this);
		dialog.setMessage("fetching friendstalker members");
		return dialog;
	}
	
	
	public void displayList()
	{
		 
		
		//LinearLayout ll = (LinearLayout) findViewById(R.id.ManageFriendsViewControllerLinearLayout);
		
		ll.removeAllViews();
		
			
		for(int index=0; index<friendList.size(); index++)
		{
			FriendProspect f = friendList.get(index);
			ll.addView(createRow(f, index), index);
		}
		
		
	}
	
	public LinearLayout createRow(FriendProspect f, int index)
	{
		LinearLayout.LayoutParams rowParam = new LayoutParams(
				LayoutParams.FILL_PARENT,
				LayoutParams.WRAP_CONTENT,
				1);
		
		LinearLayout  row = new LinearLayout(this);
		row.setLayoutParams(rowParam);
		
	
		
			row.setBackgroundDrawable(new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM,
					rowGradientColors )); 
		
		

		LinearLayout text = new LinearLayout(this);
		text.setLayoutParams(new LayoutParams(
				LayoutParams.WRAP_CONTENT,
				LayoutParams.WRAP_CONTENT,
				1));
		text.setOrientation(LinearLayout.VERTICAL);
		
		TextView name = new TextView(this);
		TextView userId = new TextView(this);
		name.setText(f.fname + " " + f.lname);
		userId.setText(f.username);
		name.setTextSize(16);
		name.setGravity(android.view.Gravity.TOP);
		text.addView(name);
		text.addView(userId);
		
		
		row.addView(text);
		
		LinearLayout buttons = new LinearLayout(this);
		buttons.setLayoutParams(new LayoutParams(
				LayoutParams.WRAP_CONTENT,
				LayoutParams.WRAP_CONTENT,
				1));
		buttons.setOrientation(LinearLayout.HORIZONTAL);
		buttons.setGravity(android.view.Gravity.RIGHT);
		
		Button actionButton = new Button(this);
		
		switch(f.status)
		{
		case FriendProspect.NOT_FRIEND:
			actionButton.setOnClickListener(new AddFriendButtonListener(index));
			actionButton.setText("add");
			break;
		
		case FriendProspect.FRIENDS_REQUEST:
			actionButton.setOnClickListener(new DenyFriendButtonListener(index));
			actionButton.setText("deny");
			Button  b2 = new Button(this);
			b2.setOnClickListener(new AcceptButtonListener(index));
			b2.setText("accept");
			buttons.addView(b2);
			break;
		
		case FriendProspect.YOUR_REQUEST:
			TextView pendingText = new TextView(this);
			pendingText.setText("pending request");
			pendingText.setTextColor(Color.YELLOW);
			buttons.addView(pendingText);
			actionButton = null;
			break;
		
		case FriendProspect.IS_FRIEND:
			actionButton.setOnClickListener(new RemoveFriendButtonListener(index));
			actionButton.setText("remove");
			
			break;
		
		default:
			break;
				
		}
		if(actionButton!=null)
			buttons.addView(actionButton);
		row.addView(buttons);
		
		
		return row;
	}
	
	
	public void getFriendList()
	{
		{
			Message msg = messageHandler.obtainMessage(WORKING);
			Bundle b = new Bundle();
			b.putBoolean("working", true);
			msg.setData(b);
			messageHandler.sendMessage(msg);
		}
		
		friendList = dh.getAllFriends(true, searchBox.getText().toString());
		
		{
			Message msg = messageHandler.obtainMessage(READY);
			Bundle b = new Bundle();
			b.putBoolean("ready", true);
			msg.setData(b);
			messageHandler.sendMessage(msg);
		}
		
	}
	
	
	
	
	
	private class FakedataHandler
	{
		
		HashMap<String, FriendProspect> fl;
		
		public FakedataHandler()
		{
			fl = new HashMap<String, FriendProspect>();
			for(int i=0; i < 40; i++)
			{
				fl.put("username"+i, new FriendProspect("username"+i, "firstname"+i, "lastname"+i, i%4));
			}
		}
		/**
		 * 
		 * @param isFriend 
		 * true will get all
		 * false will exclude friends
		 * @param searchPattern
		 * @return
		 */
		public ArrayList<FriendProspect> getAllFriends(boolean isFriend, String searchPattern)
		{
			ArrayList<FriendProspect> l = new ArrayList<FriendProspect>();
			Log.i("FakeDataHandler", "generating friendslist");
			Log.i("FakeDataHandler", "searching for "+searchPattern);
			try{
				Thread.sleep(1000);
			}
			catch(Exception e)
			{
				
			}
			if(isFriend)
				for(FriendProspect f: fl.values())
					l.add(f);
			else
				for(FriendProspect f: fl.values())
				{
					if(f.status != FriendProspect.IS_FRIEND)
						l.add(f);
				}
			return l;
		}


		public boolean addFriend(String user)
		{
			fl.get(user).status = FriendProspect.YOUR_REQUEST;
			return true;
		}
		public boolean removeFriend(String user)
		{
			fl.get(user).status = FriendProspect.NOT_FRIEND;
			return true;
		}
		public boolean answerRequest(String user, boolean accept)
		{
			FriendProspect f = fl.get(user); 
			if(f==null) 
				return false;
			else
				if(accept)
				f.status = FriendProspect.IS_FRIEND;
				else
				f.status = FriendProspect.NOT_FRIEND;
				return true;
		}
		
	}
	
	private class AcceptButtonListener implements OnClickListener
	{
		int index;
		public AcceptButtonListener(int index)
		{
			this.index = index;
		}
		public void onClick(View v) {
			friendList.get(index).status = FriendProspect.IS_FRIEND;
			dh.answerRequest((friendList.get(index).username), true); 
			ll.removeViewAt(index);
			ll.addView(createRow(friendList.get(index), index), index);
		}
		
	}
	
	private class AddFriendButtonListener implements OnClickListener
	{
		int index;
		public AddFriendButtonListener(int index)
		{
			this.index = index;
		}
		public void onClick(View v) {
			
			friendList.get(index).status = FriendProspect.YOUR_REQUEST;
			dh.addFriend(friendList.get(index).username);
			ll.removeViewAt(index);
			ll.addView(createRow(friendList.get(index), index), index);
		}
		
	}

	private class DenyFriendButtonListener implements OnClickListener
	{
		int index;
		public DenyFriendButtonListener(int index)
		{
			this.index = index;
		}
		public void onClick(View v) {
			friendList.get(index).status = FriendProspect.NOT_FRIEND;
			dh.answerRequest((friendList.get(index).username), false);
			ll.removeViewAt(index);
			ll.addView(createRow(friendList.get(index), index), index);
			
		}
		
	}
	
	private class RemoveFriendButtonListener implements OnClickListener
	{
		int index;
		public RemoveFriendButtonListener(int index)
		{
			this.index = index;
		}
		public void onClick(View v) {
			friendList.get(index).status = FriendProspect.NOT_FRIEND;
			dh.removeFriend((friendList.get(index).username));
			ll.removeViewAt(index);
			ll.addView(createRow(friendList.get(index), index), index);
		}
		
	}
	
	
}
